import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JOptionPane;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author BENSON
 */
public class ControllerAddFlight {
    Connection conn=null;
    String querry;
    Statement st=null;
   public EnterFlight addFlt;
    
    private ActionListener actionListener;
    
    public ControllerAddFlight( ){
       // this.deleteOrder = delete;
        addFlt = new EnterFlight();
        addFlt.setVisible(true);
                          
    }
   
     
    public void addFlights(){
    try{
       Class.forName("com.mysql.jdbc.Driver");
       conn =(Connection)DriverManager.getConnection("jdbc:mysql://localhost:3306/jetwingdb?zeroDateTimeBehavior=convertToNull","root","root");
       
        querry= "insert into flights (fName,ffrom,destination,Price,fCapacity,depatureTime,arrivalTime) values (?,?,?,?,?,?,?)";
        
    PreparedStatement p =conn.prepareStatement(querry);
    p.setString(1, addFlt.getFName().getText());
    p.setString(2, addFlt.getFrom().getText());
    p.setString(3, addFlt.getDestination().getText());
    p.setString(4, addFlt.getPrice().getText());
    p.setString(5, addFlt.getFCapacity().getText());
    p.setString(6, addFlt.getDepatureTime().getText());
    p.setString(7, addFlt.getArrivalTime().getText());
    p.execute();
        
    p.close();
   conn.close();
  JOptionPane.showMessageDialog(null, "Flight has been succesfully added");
    }  
    catch(Exception e){
    JOptionPane.showMessageDialog(null,e);
    }
    
    }
     public void contol(){        
        actionListener = new ActionListener() {
              public void actionPerformed(ActionEvent actionEvent) {  
                  if(actionEvent.getSource()==addFlt.getSaveButton()){
                        addFlights();
                  }
                  if(actionEvent.getSource()==addFlt.getCloseButton()){
                  addFlt.setVisible(false);
                  
                  }
              }
              
        };                
        addFlt.getSaveButton().addActionListener(actionListener);
        addFlt.getCloseButton().addActionListener(actionListener);
    }
     public static void main(String[]args){
         ControllerAddFlight c= new ControllerAddFlight();
         c.contol();
     
     }
    
}
